package com.grape.dto;

import com.grape.constants.Constants;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 类描述： UserOnlineTimeDTO类
 *
 * @author  putao
 * @email   2628279194@qq.com
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class UserOnlineTimeDTO {

    /**
     * 替换周
     * @throws ParseException   解析异常
     */
    public static void replaceWeek() throws ParseException {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        int weekIndex = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        Constants.weeks[Math.max(weekIndex, 0)] = "今天";
    }

    private String dates;

    private String week;

    private Double totalTime;

    /**
     * 设置周
     * @throws Exception 异常
     */
    public void setWeekByDates() throws Exception {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new SimpleDateFormat("yyyy-MM-dd").parse(this.dates));
        int weekIndex = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        if (weekIndex < 0) {
            weekIndex = 0;
        }
        this.week = Constants.weeks[weekIndex];
    }

    /**
     * 设置总时长
     * @param type 类型
     */
    public void setTotalTimeByType(String type) {
        if (this.totalTime == null) {
            return;
        }
        this.totalTime = new BigDecimal(this.totalTime / ( Constants.TIME_HOUR.equals(type) ? 3600 : 60)).setScale(1, RoundingMode.HALF_UP).doubleValue();
    }
}
